#!/usr/bin/env perl
# For use my various monitoring systems - nagios / zabbix etc

use Getopt::Std;
use Data::Dumper;

my $arrcmd = "/opt/HPQacucli/sbin/hpacucli";
my $slotnr = 0;
my $pdcnt = 0;
my $pdokcnt = 0;

my $debug = 0;

sub findslotnr {
	open(HPCMD, "$arrcmd ctrl all show status |");
	while(<HPCMD>) {
		next unless ($_=~ /Slot/);
		my @tmp = split(' ', $_);
		$slotnr = $tmp[5];
	}
	close(HPCMD);		
}

sub countpd {
	open(HPCMD, "$arrcmd ctrl slot=$slotnr pd all show status |");
	while(<HPCMD>) {
		next unless ($_ =~ /physicaldrive/);
		$pdcnt++;
	}
	close(HPCMD);
}

sub checkpd {
        open(HPCMD1, "$arrcmd ctrl slot=$slotnr pd all show status |");
        while(<HPCMD1>) {
                next unless ($_ =~ /physicaldrive/);
		next unless ($_ =~ /OK/);
		$pdokcnt++;
	}
	close(HPCMD1);
}

findslotnr();
countpd();
checkpd();

print "Controller in slot $slotnr has $pdcnt physical drives, $pdokcnt are apparently OK\n" if $debug;

if ($pdokcnt != $pdcnt) {
	print "0\n"; exit 1;
} else {
	print "1\n"; exit 0;
}
